package dongtai;

public class qiyisi {
    public static int maxProfit(int[] prices, int fee) {
        int n=prices.length;
        if(n==1){
            return 0;
        }
        int[] f=new int[n];
        int[] g=new int[n];
        f[0]=-prices[0];
        g[0]=0;
        for(int i=1;i<n;i++){
            f[i]=Math.max(f[i-1],g[i-1]-prices[i]);
            g[i]=Math.max(g[i-1],f[i-1]+prices[i]-fee);
        }
        return Math.max(f[n-1],g[n-1]);

    }

    public static void main(String[] args) {
        int[] prices = {1, 3, 2, 8, 4, 9};
        int fee = 2;
        int i = maxProfit(prices, fee);
        System.out.println(i);

    }
}
